from flask import Blueprint, make_response
from flask_restful import Api, Resource, reqparse, fields, marshal_with
from apps.models.device import Device
from exts import db

test_bp1 = Blueprint('aa', __name__)
api = Api(test_bp1)

unit_field = {
    'last_update_time': fields.String,
    'facility': fields.String,
    'state': fields.String
}


class TestAPI(Resource):
    @marshal_with(unit_field)
    def get(self):
        datas = Device.query.all()
        return datas


class Test2API(Resource):
    def get(self):
        with open('D:\code\PycharmProjects\PythonProjects\\flaskProject\static\image\\ft2.png', 'rb') as f:
            img = f.read()
        response = make_response(img)
        response.headers['Content-Type'] = 'image/png'  # 返回的内容类型必须修改
        return response


api.add_resource(TestAPI, '/aa')
api.add_resource(Test2API, '/bb')